#define _CRT_SECURE_NO_WARNINGS 1
#include <iostream>
#include <vector>
#include <unordered_map>

using namespace std;

class Solution
{
public:
    bool containsNearbyDuplicate(vector<int>& nums, int k)
    {
        unordered_map<int, int> hash;
        for (int i = 0; i < nums.size(); i++)
        {
            if (hash.count(nums[i]))
            {
                if (abs(i - hash[nums[i]]) > k)
                {
                    hash[nums[i]] = i;
                    continue;
                }
                return true;
            }
            else
                hash[nums[i]] = i;
        }
        return false;
    }
};